package com.yeyks.app.controller.base.vip;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.yeyks.app.annotation.LoadUserId;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.commonReference.pojo.param.vip.MemberCardOnline.MemberCardOnlineSetPasswordParam;
import com.yeyks.commonReference.service.vip.MemberCardOnlineService;
import com.yeyks.vip.param.MemberCardEnableRecordCreateOrderPayRecordParam;
import com.yeyks.vip.param.MemberCardOnlinePlaceAnOrderAndBindParam;
import com.yeyks.vip.param.MemberCardOnlinePlaceAnOrderParam;
import com.yeyks.vip.param.MemberCardOnlinePlaceAnOrderServiceParam;
import com.yeyks.vip.vo.MemberCardEnableOrderPlaceAnOrderVo;
import com.yeyks.vip.vo.MemberCardOnlineCheckEnableCardVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

@Api(tags = "线上会员卡购买", description = "线上会员卡购买")
@RestController
@RequestMapping("/app/member/card/online")
public class MemberCardOnlineController {

    @Autowired
    private MemberCardOnlineService memberCardOnlineService;

    @ApiOperation(value = "检查是否有卡可以出售(线上):v3.8.3")
    @PostMapping(value = "/check")
    public AjaxResponseBody<MemberCardOnlineCheckEnableCardVo> checkEnableCard(Integer cardType) {
        if (Objects.isNull(cardType)) {
            cardType = 2;
        }
        return AjaxResponseBody.success(memberCardOnlineService.checkEnableCard(cardType));
    }

    @ApiOperation(value = "下单不绑卡(线上):v3.8.3")
    @PostMapping(value = "/placeAnOrder")
    public AjaxResponseBody<MemberCardEnableOrderPlaceAnOrderVo> placeAnOrder(@Validated MemberCardOnlinePlaceAnOrderParam param) {
        return AjaxResponseBody.success(memberCardOnlineService.placeAnOrder(BeanUtil.toBean(param, MemberCardOnlinePlaceAnOrderServiceParam.class)));
    }

    @ApiOperation(value = "下单绑卡,创客(线上):v4.3.0")
    @PostMapping(value = "/placeAnOrderAndBind")
    public AjaxResponseBody<MemberCardEnableOrderPlaceAnOrderVo> placeAnOrderAndBind(@LoadUserId @Validated MemberCardOnlinePlaceAnOrderAndBindParam param) {
        return AjaxResponseBody.success(memberCardOnlineService.placeAnOrder(BeanUtil.toBean(param, MemberCardOnlinePlaceAnOrderServiceParam.class)));
    }

    @ApiOperation(value = "根据订单生成支付信息和支付记录(线上):v3.8.3")
    @PostMapping(value = "/createOrderPayRecord")
    public AjaxResponseBody<Map<String, Object>> createOrderPayRecord(HttpServletRequest request, MemberCardEnableRecordCreateOrderPayRecordParam param) {
        param.setIp(ServletUtil.getClientIP(request));
        return AjaxResponseBody.success(memberCardOnlineService.createOrderPayRecord(param));
    }

    @ApiOperation(value = "线上卡初始密码设置:v4.3.0")
    @PostMapping(value = "/setPassword")
    public AjaxResponseBody<MemberCardEnableOrderPlaceAnOrderVo> setPassword(@LoadUserId @Validated MemberCardOnlineSetPasswordParam param) {
        memberCardOnlineService.setPassword(param);
        return AjaxResponseBody.success();
    }

}
